import 'package:flutter/material.dart';
import '../modal/contact.dart' show Contact, mockContacts;
import '../constants.dart' show AppColors, Constants, AppStyles;

class ContactPage extends StatefulWidget {
  @override
  _ContactPageState createState() => _ContactPageState();
}

class _ContactPageState extends State<ContactPage> {
  final List<Contact> _contacts = [];

  final List<_ContactItem> _functionButtons = [
    _ContactItem(
      avatar: 'assets/images/ic_new_friend.png',
      title: '新的朋友',
      onPressed: () {
        print("点击了新的朋友");
      },
    ),
    _ContactItem(
        avatar: 'assets/images/ic_group_chat.png',
        title: '群聊',
        onPressed: () {
          print('点击了群聊。');
        }),
    _ContactItem(
        avatar: 'assets/images/ic_tag.png',
        title: '标签',
        onPressed: () {
          print('标签');
        }),
    _ContactItem(
        avatar: 'assets/images/ic_public_account.png',
        title: '公众号',
        onPressed: () {
          print('添加公众号');
        }),
  ];

  @override
  void initState() {
    super.initState();
    _contacts..addAll(mockContacts)..addAll(mockContacts)..addAll(mockContacts);
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        // 4个功能按钮
        if (index < _functionButtons.length) {
          return _functionButtons[index];
        }
        // 联系人列表
        int contactIndex = index - _functionButtons.length;
        Contact contact = _contacts[contactIndex];
        return _ContactItem(
          avatar: contact.avatar,
          title: contact.name,
          groupTitle: contact.nameIndex,
        );
      },
      itemCount: _contacts.length + _functionButtons.length,
    );
  }
}

class _ContactItem extends StatelessWidget {
  final String avatar;
  final String title;
  final String groupTitle;
  final VoidCallback onPressed;

  bool get _isAvatarFromNet {
    if (this.avatar.indexOf('http') == 0 || this.avatar.indexOf('https') == 0) {
      return true;
    } else {
      return false;
    }
  }

  _ContactItem(
      {@required this.avatar,
      @required this.title,
      this.groupTitle,
      this.onPressed})
      : assert(avatar != null),
        assert(title != null);

  @override
  Widget build(BuildContext context) {
    // 修饰左边的图片
    Widget _avatarIcon;

    if (_isAvatarFromNet) {
      _avatarIcon = Container(
        width: Constants.ContactAvatarSize,
        height: Constants.ContactAvatarSize,
        decoration: BoxDecoration(
            image:
                DecorationImage(image: NetworkImage(avatar), fit: BoxFit.cover),
            borderRadius: BorderRadius.all(Radius.circular(5.0))),
      );
    } else {
      _avatarIcon = Container(
        width: Constants.ContactAvatarSize,
        height: Constants.ContactAvatarSize,
        decoration: BoxDecoration(
            image:
                DecorationImage(image: AssetImage(avatar), fit: BoxFit.cover),
            borderRadius: BorderRadius.all(Radius.circular(5.0))),
      );
    }

    return Container(
      padding:
          const EdgeInsets.only(left: 16.0, right: 16.0, top: 0.0, bottom: 0.0),
      child: Container(
        padding: const EdgeInsets.only(top: 10.0, bottom: 10.0),
        child: Row(
          children: <Widget>[_avatarIcon, SizedBox(width: 10.0), Text(title)],
        ),
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    width: Constants.DividerWidth,
                    color: Color(AppColors.DividerColor)))),
      ),
    );
  }
}
